home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacWT 0.9 / wt Mac Source / AEHandler.c next >
Encoding:
C/C++ Source or Header  |  1995-10-10  |  3.7 KB  |  198 lines  |  [TEXT/CWIE]

  1. /*
  2. ** File:        AEHandler.c
  3. **
  4. ** Written by:    Bill Hayden
  5. **                Nikol Software
  6. **
  7. ** Copyright © 1995 Nikol Software
  8. ** All rights reserved.
  9. */
  10.  
  11.  
  12.  
  13. #include "AEHandler.h"
  14. #include "Failure.h"
  15. #include "MacWT.h"
  16. #include "StringUtils.h"
  17. #include "FileUtils.h"
  18.  
  19.  
  20. #if GENERATINGPOWERPC
  21. #pragma options align=mac68k
  22. #endif
  23. struct AEHandler{
  24.     AEEventClass            theEventClass;
  25.     AEEventID                theEventID;
  26.     AEEventHandlerProcPtr    theHandler;
  27.     AEEventHandlerUPP        theUPP;
  28. };
  29. typedef struct AEHandler AEHandler;
  30. #if GENERATINGPOWERPC
  31. #pragma options align=reset
  32. #endif
  33.  
  34.  
  35. static AEHandler keywordsToInstall[] = {
  36.     { kCoreEventClass,    kAEOpenApplication,    (AEEventHandlerProcPtr)OpenApplicationEvent,    nil },
  37.     { kCoreEventClass,    kAEOpenDocuments,    (AEEventHandlerProcPtr)OpenDocumentEvent,        nil },
  38.     { kCoreEventClass,    kAEQuitApplication,    (AEEventHandlerProcPtr)QuitApplicationEvent,    nil }
  39. };
  40.  
  41. #define kNumKeywords (sizeof(keywordsToInstall) / sizeof(AEHandler))
  42.  
  43.  
  44. static OSErr    GotRequiredParams(AppleEvent *);
  45.  
  46.  
  47.  
  48.  
  49. /*****************************************************************************/
  50.  
  51.  
  52.  
  53.  
  54. void    InitAppleEvents(void)
  55. {
  56.     OSErr    err;
  57.     short    i;
  58.  
  59.     for (i = 0; i < kNumKeywords; ++i)
  60.         {
  61.         if (!keywordsToInstall[i].theUPP)
  62.             keywordsToInstall[i].theUPP = NewAEEventHandlerProc(keywordsToInstall[i].theHandler);
  63.  
  64.         err = AEInstallEventHandler(
  65.             keywordsToInstall[i].theEventClass,    /* What class to install.  */
  66.             keywordsToInstall[i].theEventID,    /* Keywords to install.    */
  67.             keywordsToInstall[i].theUPP,        /* The AppleEvent handler. */
  68.             0L,                                    /* Unused refcon.           */
  69.             false                                /* Only for our app.       */
  70.             );
  71.  
  72.         if (err)
  73.             {
  74.             Fail(err, __FILE__, __LINE__, TRUE);
  75.             return;
  76.             }
  77.         }
  78.     
  79. }
  80.  
  81.  
  82.  
  83.  
  84. /*****************************************************************************/
  85.  
  86.  
  87.  
  88.  
  89. void DoAppleEvent(EventRecord *event)
  90. {
  91.     OSErr err;
  92.     
  93.     err = AEProcessAppleEvent(event);
  94.     if ( err )
  95.         Fail(err, __FILE__, __LINE__, FALSE);
  96. }
  97.  
  98.  
  99.  
  100.  
  101. /*****************************************************************************/
  102.  
  103.  
  104.  
  105.  
  106. static OSErr GotRequiredParams(AppleEvent *theAE)
  107. {
  108.     OSErr        err;
  109.     DescType    returnedType;
  110.     Size        actualSize;
  111.  
  112.     err = AEGetAttributePtr(theAE, keyMissedKeywordAttr, typeWildCard, &returnedType, nil, 0, &actualSize);
  113.     if ( err == errAEDescNotFound )
  114.         return noErr;
  115.     else if ( err == noErr )
  116.         return errAEParamMissed;
  117. }
  118.     
  119.     
  120.     
  121.  
  122. /*****************************************************************************/
  123.  
  124.  
  125.  
  126.  
  127. pascal OSErr OpenDocumentEvent(AppleEvent *theAE, AppleEvent *reply, long handlerRefcon)
  128. {
  129.     FSSpec        myFSS;
  130.     AEDescList    docList;
  131.     OSErr        err;
  132.     long        itemsInList;
  133.     Size        actualSize;
  134.     AEKeyword    keyword;
  135.     DescType    returnedType;
  136.     
  137.     err = AEGetParamDesc(theAE, keyDirectObject, typeAEList, &docList);
  138.     if ( err == noErr )
  139.         {
  140.         err = GotRequiredParams(theAE);
  141.         if ( err == noErr )
  142.             {
  143.             err = AECountItems(&docList, &itemsInList);
  144.             if ( err == noErr )
  145.                 {
  146.                 err = AEGetNthPtr(&docList, 1, typeFSS, &keyword, &returnedType, (Ptr)&myFSS, sizeof(myFSS), &actualSize);
  147.                 if ( err == noErr )
  148.                     {
  149.                     PathNameFromDirID(myFSS.vRefNum, myFSS.parID, gWorldFile);
  150.                     pcat(gWorldFile, myFSS.name);
  151.                     }
  152.                 }
  153.             }
  154.         err = AEDisposeDesc(&docList);
  155.         }
  156.     return err;
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /*****************************************************************************/
  163.  
  164.  
  165.  
  166.  
  167. pascal OSErr OpenApplicationEvent(AppleEvent *theAE, AppleEvent *reply, long handlerRefcon)
  168. {
  169.     OSErr    err;
  170.  
  171.     err = GotRequiredParams(theAE);
  172.     if (!err)
  173.         {
  174.         }
  175.  
  176.     return err;
  177. }
  178.  
  179.  
  180.  
  181.  
  182. /*****************************************************************************/
  183.  
  184.  
  185.  
  186.  
  187. pascal OSErr QuitApplicationEvent(AppleEvent *theAE, AppleEvent *reply, long handlerRefcon)
  188. {
  189.     OSErr err;
  190.     extern Boolean quitting;
  191.  
  192.     err = GotRequiredParams(theAE);
  193.     if ( !err )
  194.         quitting = TRUE;
  195.     
  196.     return err;
  197. }
  198.